<?php

class Student_Controller extends BaseController {
	public function index(Request $request, Response $response) {
		$student = Student::load($this->user->internalId);
		$response->append(new View('student/personalInfo', compact('student')));
	}

	public function courseRecord(Request $request, Response $response) {
		$student = Student::load($this->user->internalId);
		$courseSelections = CourseSelection::find(new Query('studentId', $student->id), ['nolimit' => 1]);
		if (empty($courseSelections)) $courseSelections = array();
		$response->append(new View('student/courseRecord', compact('courseSelections')));
	}

	public function score(Request $request, Response $response) {
		$student = Student::load($this->user->internalId);
		$courseSelections = CourseSelection::find(new Query('studentId', $student->id), ['nolimit' => 1]);
		$response->append(new View('student/score', compact('courseSelections')));
	}

	//选课页
	public function select(Request $request, Response $response) {
		/**
		 * @var CourseSelection $courseSelection
		 * @var CourseClass $courseClass
		 */
		$student = Student::load($this->user->internalId);
		$courseClasses = CourseClass::find(new TrueQuery(), ['nolimit' => 1]);
		$courseSelections = CourseSelection::find(new Query('studentId', $student->id), ['nolimit' => 1]);
		$courseClasses = object_array_diff($courseClasses, CourseClass::find(new InQuery('id', object_map($courseSelections, 'courseClassId') ?: ['']), ['nolimit' => 1]));
		$classTimes = [];
		foreach ($courseClasses as $courseClass) $classTimes[$courseClass->id] = ClassTime::find(new Query('classId', $courseClass->id), ['nolimit' => 1]);
		foreach ($courseSelections as $courseSelection) {
			$courseClass = CourseClass::load($courseSelection->courseClassId);
			$classTimes[$courseClass->id] = ClassTime::find(new Query('classId', $courseClass->id), ['nolimit' => 1]);
		}
		$response->append(new View('student/select', compact('courseClasses', 'student', 'courseSelections', 'classTimes')));
	}

	public function add(Request $request, Response $response) {
		$classCourseId = $request->classCourseId;
		$student = Student::load($this->user->internalId);
		$courseSelections = CourseSelection::find(new Query('studentId', $student->id), ['nolimit' => 1]);
		$courseClassIds = [];
		$classTimes = [];
		foreach ($courseSelections as $courseSelection) {
			$courseClassIds[] = $courseSelection->courseClassId;
			$cS = CourseClass::load($courseSelection->courseClassId);
			$classTimes[$cS->id] = ClassTime::find(new Query('classId', $cS->id), ['nolimit' => 1]);
		}
		if (in_array($classCourseId, $courseClassIds)) {
			$response->alertAndGoTo('该课程已经选择！', '/student/select/');
		} else {
			$conflictClasses = CourseClass::studentSelect($student->id, $classCourseId);
			if ($conflictClasses === true) $response->alertAndGoTo('选课成功!', '/student/select/');
			else $response->alertAndGoTo("选课失败!与下列课程冲突:\\n" . implode(',', object_map($conflictClasses, 'name')) . "", '/student/select/');
		}
	}

	public function remove(Request $request, Response $response) {
		$courseSelectionId = $request->get('courseSelectionId');
		$student = Student::load($this->user->internalId);
		$courseSelection = CourseSelection::load($courseSelectionId);
		if ($courseSelection) $courseSelection->remove();
		else $response->alertAndGoTo('该课程已删除！', '/student/select/');
		$courseSelections = CourseSelection::find(new Query('studentId', $student->id), ['nolimit' => 1]);
		$classTimes = [];
		if (!$courseSelections) $courseSelections = array();
		foreach ($courseSelections as $courseSelection) {
			$cS = CourseClass::load($courseSelection->courseClassId);
			$classTimes[$cS->id] = ClassTime::find(new Query('classId', $cS->id), ['nolimit' => 1]);
		}
		$response->alertAndGoTo('删除成功!', '/student/select/');
	}

	//处理提交的选课表单
	public function courseSelect(Request $request, Response $response) {
		$student = Student::load($this->user->internalId);
		$courseSelections = CourseSelection::find(new Query('studentId', $student->id), ['nolimit' => 1]);
		$response->append(new View('student/courseRecord', compact('courseSelections')));
	}

}